
<template>
  <div v-if="shipment.planType === PLAN_TYPE_FL()">
    <el-button
      :loading="loading"
      @click="showDialog"
      type="primary"
    >{{$t('common.export')}}</el-button>
    <el-dialog
      title="请选择导出的内容"
      width="350px"
      :visible.sync="dialog"
      :append-to-body="true"

    >
      <div style="margin-bottom: -10px">
        <el-radio-group v-model="currentType">
          <el-radio :label="1">装箱单</el-radio>
          <el-radio :label="2">重量明细</el-radio>
          <el-radio :label="3">箱唛</el-radio>
        </el-radio-group>
        <div class="mgt20" style="text-align: right">
          <el-button @click="dialog = false">取消</el-button>
          <el-button type="primary" @click="exportFile">确认</el-button>
        </div>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import {getShipmentA4PDFById} from "@/api/shipment";
import {exportShipmentBoxExcelById} from "@/api/shipmentV2";
import {PLAN_TYPE_FL} from "@/utils/shippingPlan";
import request from "@/utils/request";

export default {
  name: "ExportShipmentTemplate",
  props:{
    shipment: {
      type: Object,
      required: true,
    }
  },
  data(){
    return {
      loading: false,
      dialog: false,
      currentType: 1,
    }
  },
  methods:{
    PLAN_TYPE_FL() {
      return PLAN_TYPE_FL
    },
    showDialog(){
      this.dialog = true
    },
    exportFile(){
      this.dialog = false
      switch (this.currentType){
        case 1:
          this.getShipmentTemplate()
          break
        case 2:
          this.getShipmentWeightTemplate()
          break
        case 3:
          this.getShipmentBoxTemplate()
          break
      }
    },
    getShipmentTemplate(){
      this.loading = true
      exportShipmentBoxExcelById(this.shipment.id)
        .then(res=>{
          this.loading = false
          window.open(res.data.url,"_blank");
        })
    },
    getShipmentWeightTemplate(){
      this.loading = true
      request({
        url: `/shipmentV2/exportShipmentWeightBoxExcelById/${this.shipment.id}`
      })
        .then(res=>{
          this.loading = false
          window.open(res.data.url,"_blank");
        })
    },
    getShipmentBoxTemplate(){
      this.loading = true
      request({
        url: `/shipmentV2/downloadShipmentBoxLabel/${this.shipment.id}`,
        responseType: "blob", // 需要加上
        isNotJson: true,
        timeout: 60000 * 5, // 1分钟
        handleError: true,
      })
        .then(res=>{
          this.loading = false
          if(res.type === 'application/json'){
            this.$message({
              message: '下载失败，请稍后再尝试',
              duration : 5000,
              type: 'error',
              showClose: true,
            });
            return;
          }

          const pdfFile = window.URL.createObjectURL(
            new Blob([res.data], { type: "application/pdf" })
          );
          // 跳转页面预览
          window.open(pdfFile);
          // 60秒后释放
          setTimeout(()=>{
            URL.revokeObjectURL(pdfFile); //释放URL对象
          },60000)
        })
    },
  }
}
</script>

<style scoped lang="scss">

</style>
